﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Form01
{
    public partial class Users : Form
    {
        public Users()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            //查找
            //输入框
            var name = textBox1.Text;
            //数据库模糊查询代码
            var sql = string.Format("select * from Artycles where Title  like '%{0}%' ", name);
            //连接数据库
            var dt = DbHelper.GetDataTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //更新
            var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
            var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
            var author = (string)dataGridView1.SelectedRows[0].Cells["Autbhor"].Value;
            var createdTime = (string)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;
            Form4 form4 = new Form4(id, title, author, createdTime);
            var res = form4.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var name = textBox1.Text;
                var sqlStu = string.Format("select * from Artycles where Title like '%{0}%'", name);
                var dt = DbHelper.AddOrEditOrDeleteSave(sqlStu);
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("未选择任何行");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //添加
            Form4 form4 = new Form4();
            var res = form4.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var name = textBox1.Text;
                var sqlStu = string.Format("select * from Artycles where Title like '%{0}%'", name);
                var dt = DbHelper.AddOrEditOrDeleteSave(sqlStu);
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("This is NO");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //删除
        }

        private void Form3_Load(object sender, EventArgs e)
        {
          
        }

        private void Users_Load(object sender, EventArgs e)
        {
            string cmdString = "select * from Artycles";
            var dt = DbHelper.GetDataTable(cmdString);
            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
        }
    }
}
